package com.campus_information_portal.controller;

import com.campus_information_portal.entity.ExemStuAcaTerm;
import com.campus_information_portal.entity.Exemption;
import com.campus_information_portal.entity.ExemptionList;
import com.campus_information_portal.service.ExemptionService;
import com.campus_information_portal.util.Message;
import com.campus_information_portal.util.Msg;
import com.campus_information_portal.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 20155873 张联富
 * @Description $Description$
 * @methodName $methodName$
 * @date $date$ $time$
 * @return $returns$
 */
@Controller
@RequestMapping("/exemption")
@ResponseBody
@CrossOrigin(origins = "*")
public class ExemptionController  {
    @Autowired
    private ExemptionService exemptionService;
    private ExemStuAcaTerm exemStuAcaTerm = new ExemStuAcaTerm();
    private Exemption exemption = new Exemption();
    private ExemptionList l = new ExemptionList();

    //是否能申请免修
    @RequestMapping(value = "/isApply", method = RequestMethod.GET)
    public Message queryDormitoryApplyForm(int stuInfoId) {

        System.out.println(stuInfoId);

        Message m = new Message();

        int flag = exemptionService.IsApply(stuInfoId);
        System.out.println("+++***"+flag);
        List<ExemStuAcaTerm> list = (List<ExemStuAcaTerm>) exemptionService.selectExemStuAcaTermByid(stuInfoId);
        System.out.println(list.size());
        if (flag == 0) { // flag=0; 6级未参加
            m.setCode(0);
            m.setMsg("抱歉，您还未参加过6级考试，无资格申请");
            //m.add("6级成绩", exemptionService.queryCet6(stuInfoId));
        }

        if (flag == 1) {// flag=1; 6级参加过，但没达到425
            m.setCode(1);
            m.setMsg("抱歉，您的6级成绩未达到425，无资格申请");
            m.add("cet6", exemptionService.queryCet6(stuInfoId));
        }
        if (flag == 2) { //flag = 2;6级通过，但从未申请过，有资格申请
            m.setCode(2);
            m.setMsg("请进行第一次申请");
            m.add("cet6", exemptionService.queryCet6(stuInfoId));//6级成绩
            m.add("score", judgeCet6(exemptionService.queryCet6(stuInfoId)));//折合后成绩
        }
        if (flag == 3) { //flag = 3；申请被批准 ，无法再次申请
            m.setCode(3);
            m.setMsg("您的申请免修已被批准，无须再次申请");
            m.add("ExemptionApplyList",list);//申请记录
            m.add("cet6", exemptionService.queryCet6(stuInfoId));//6级成绩
            m.add("score", judgeCet6(exemptionService.queryCet6(stuInfoId)));//折合后成绩
        }
        if (flag == 4) {//flag = 4；正在处理中，无法再次申请
            m.setCode(4);
            m.setMsg("您的免修申请正在处理中，请耐心等待");
            m.add("ExemptionApplyList",list);//申请记录
            m.add("cet6", exemptionService.queryCet6(stuInfoId));//6级成绩
            m.add("score", judgeCet6(exemptionService.queryCet6(stuInfoId)));//折合后成绩
        }
        if (flag == 5) { //flag=5;  //2月  78月 为放假月，不管是否有申请资格，无法进行申请
            m.setCode(5);
            m.setMsg("假期内无法申请，请在开学后申请");
            if(list.size()>0){
                m.add("ExemptionApplyList",list);
                m.add("cet6", exemptionService.queryCet6(stuInfoId));//6级成绩
                m.add("score", judgeCet6(exemptionService.queryCet6(stuInfoId)));//折合后成绩
            }
        }
        if (flag == 6) {//flag = 6; //可以再次申请
            m.setCode(6);
            m.setMsg("您可以再次申请");
            m.add("ExemptionApplyList",list);
            m.add("cet6", exemptionService.queryCet6(stuInfoId));//6级成绩
            m.add("score", judgeCet6(exemptionService.queryCet6(stuInfoId)));//折合后成绩
        }
        if (flag == 7) {// flag = 7; //不可以申请
            m.setCode(7);
            m.setMsg("抱歉，同一学期不能申请第二次，请等到下学期及以后再申请");
            m.add("ExemptionApplyList",list);
            m.add("cet6", exemptionService.queryCet6(stuInfoId));//6级成绩
            m.add("score", judgeCet6(exemptionService.queryCet6(stuInfoId)));//折合后成绩
        }
        return m;
    }



    //申请免修
    @RequestMapping(value = "/Apply", method = RequestMethod.GET)
    public Message ApplyExemption(int stuInfoId) {
        exemptionService.addExemption(stuInfoId);
        Message m = new Message();
        List<ExemStuAcaTerm> list = (List<ExemStuAcaTerm>) exemptionService.selectExemStuAcaTermByid(stuInfoId);
        m.add("ExemptionApplyList",list);
        m.setMsg("申请成功，请静候佳音");
        m.add("cet6", exemptionService.queryCet6(stuInfoId));//6级成绩
        m.add("score", judgeCet6(exemptionService.queryCet6(stuInfoId)));//折合后成绩
        return m;
    }

    //老师查看全部免修记录（未批准:0   批准：1   退回：2）
    @RequestMapping(value = "/QueryApply", method = RequestMethod.GET)
    public Message QueryApply(int pageIndex, int exemption_flag) {
        System.out.println(pageIndex);
        System.out.println(exemption_flag);
        PageInfo<ExemStuAcaTerm> pageinfo = new PageInfo<ExemStuAcaTerm>();
        pageinfo.setPageIndex(pageIndex);
        exemptionService.queryAllEmp(pageinfo, exemption_flag);
        Message m = new Message();
        m.add("pageinfo",pageinfo);
        return m;
    }

    //老师查看某个同学的免修记录（未批准）
    @RequestMapping(value = "/QueryOneApply", method = RequestMethod.GET)
    public Msg QueryOneApply(int exemption_id) {
        ExemStuAcaTerm esa = exemptionService.queryOneExe(exemption_id).get(0);

        Msg m = new Msg();
        m.setData(esa);
        return m;
    }

    //批准
    @RequestMapping(value = "/sanctionApply", method = RequestMethod.GET)
    public Msg sanctionApply(int exemption_id) {
        //批准
        exemptionService.manageExemption(exemption_id, 1);

        //所有未批准免修结果的第一页
        PageInfo<ExemStuAcaTerm> pageinfo = new PageInfo<ExemStuAcaTerm>();
        pageinfo.setPageIndex(1);
        exemptionService.queryAllEmp(pageinfo, 0);
        Msg m = new Msg();
        m.setData(pageinfo);


        return m;
    }

    //退回
    @RequestMapping(value = "/retreatApply", method = RequestMethod.GET)
    public Msg retreatApply(int exemption_id) {
        //退回
        exemptionService.manageExemption(exemption_id, 2);

        //所有未批准免修结果的第一页
        PageInfo<ExemStuAcaTerm> pageinfo = new PageInfo<ExemStuAcaTerm>();
        pageinfo.setPageIndex(1);
        exemptionService.queryAllEmp(pageinfo, 0);
        Msg m = new Msg();
        m.setData(pageinfo);

        return m;
    }

    //select
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public Message select(String info) {
        System.out.println(info);
        List list = new ArrayList();
        list.add("mian'xiu'dan");
        list.add("liu'su'dan");
        list.add("mian'xiu'shen'qing");
        list.add("di'fang'you");
        List list1 = new ArrayList();
        // if("sss".startsWith("sf")){}
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).toString().startsWith(info)) {
                list1.add(list.get(i));
            }
        }

        //select
        Message m = new Message();

        return m.success().add("list", list1);
    }
    public int  judgeCet6(int cet6){
        //425-450  75
        //450-500 80
        //500-550  85
        // 550-600   90
        //600-650  95
        //650-700   100
        int score=0;
        if(cet6>=425&&cet6<450){
            score=75;
        }
        if(cet6>=450&&cet6<500){
            score=80;
        }
        if(cet6>=500&&cet6<550){
            score=85;
        }
        if(cet6>=550&&cet6<600){
            score=90;
        }
        if(cet6>=600&&cet6<650){
            score=95;
        }
        if(cet6>=650&&cet6<=700){
            score=100;
        }
        return score;
    }
}
